package com.qf.web.controller;


import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.StringUtils;
import com.qf.web.common.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();

    protected String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");

        //判断数据库中是否有数据 登录
        User users = userService.loginByNameAndPwd(userName, userPwd);
        if (users != null) {
            req.getSession().setAttribute("users", users);
            return "success";
        } else {
            return "error";
        }

    }

    protected String register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端数据
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        User user=userService.findUserName(userName);
        //调用业务逻辑
        if (user==null){
            userService.insert(userName, userPwd);
            return "success";
        }else{
            return "error";
        }
    }
    protected String loginout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("users");
        session.invalidate();
        return "redirect:/pages/user/login.jsp";
    }


}
